import { PlaneGeometry, Color } from "three";
import { Mesh } from "three/src/objects/Mesh";
import { App } from "../../application";
import Events from "../../application/constants/Events";
import Const from "../../application/constants/Const";
import { CustomMeshBasicMaterial } from "../../renderExtension/Material/CustomMaterial";

/**
 * 地图的虚拟底面, 用于避免地图拖拽的时候无法进行射线拾取。另外用于避免射线拾取时的抖动问题
 */
class MapVirtualCarpet extends Mesh {
    private app;

    constructor() {
        super();

        this.app = App.getInstance();

        let tileWidth = Const.MapConst.tileWidth;
        this.geometry = new PlaneGeometry(tileWidth, tileWidth, 1, 1);

        let materialParam = {coreParameters: {colorWrite: false, depthWrite: false}, identity: 'backgroundFog'};
        this.material = new CustomMeshBasicMaterial(materialParam);
        this.app.on(Events.CameraChange, this.changeCameraPosition.bind(this), this);
    }

    private changeCameraPosition() {
        let oldZ = this.position.z;
        let carpetPosition = this.app.camera.lookAtTarget.clone();
        // carpetPosition.x = carpetPosition.x % Const.MapConst.tileWidth;
        // carpetPosition.y = carpetPosition.y % Const.MapConst.tileWidth;
        carpetPosition.z = oldZ;
        this.position.copy(carpetPosition);
    }

    public adjustZ(z: number) {
        this.position.z = z;
    }
}
export { MapVirtualCarpet as MapCarpet }